<template>
  <div class="user-login">
    <div class="login-container">
      <div class="login-box">
        <!-- 左侧装饰图 -->
        <div class="login-left">
          <img src="https://picsum.photos/600/800?random=10" alt="background" class="bg-image">
          <div class="overlay">
            <h2>智慧旅游服务平台</h2>
            <p>让旅行更轻松，让服务更贴心</p>
          </div>
        </div>
        
        <!-- 右侧登录表单 -->
        <div class="login-right">
          <div class="login-form-container">
            <h3>欢迎登录</h3>
            <p class="login-desc">请输入您的账号和密码</p>
            
            <el-form 
              :model="loginForm" 
              :rules="rules" 
              ref="loginForm" 
              class="login-form">
              <el-form-item prop="username">
                <el-input 
                  v-model="loginForm.username"
                  prefix-icon="el-icon-user"
                  placeholder="请输入用户名">
                </el-input>
              </el-form-item>
              <el-form-item prop="password">
                <el-input 
                  type="password" 
                  v-model="loginForm.password"
                  prefix-icon="el-icon-lock"
                  placeholder="请输入密码"
                  @keyup.enter.native="handleLogin">
                </el-input>
              </el-form-item>
              
              <div class="remember-forgot">
                <el-checkbox v-model="rememberMe">记住我</el-checkbox>
                <el-button type="text" class="forgot-btn">忘记密码？</el-button>
              </div>
              
              <el-button 
                type="primary" 
                @click="handleLogin" 
                :loading="loading"
                class="login-button">
                {{ loading ? '登录中...' : '登 录' }}
              </el-button>
            </el-form>
            
            <div class="login-footer">
              <p>还没有账号？ <el-button type="text" @click="$router.push('/register')" class="register-btn">立即注册</el-button></p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/utils/request'

export default {
  name: 'UserLogin',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rememberMe: false,
      loading: false,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          try {
            this.loading = true;
            const response = await request.post('/api/user/login', this.loginForm);
            
            if (response.token && response.user) {
              // 确保用户信息被正确保存
              localStorage.setItem('token', response.token);
              localStorage.setItem('user', JSON.stringify(response.user));
              
              console.log('Login successful:', response.user);
              
              this.$message.success('登录成功');
              
              // 根据用户角色跳转
              if (response.user.role === 'admin') {
                this.$router.push('/admin/emergency');
              } else {
                this.$router.push('/');
              }
            }
          } catch (error) {
            console.error('Login failed:', error);
            this.$message.error(error.message || '登录失败');
          } finally {
            this.loading = false;
          }
        }
      });
    }
  }
}
</script>

<style scoped>
.user-login {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
}

.login-container {
  width: 1000px;
  height: 600px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
}

.login-box {
  display: flex;
  height: 100%;
}

.login-left {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.bg-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(0,0,0,0.3), rgba(0,0,0,0.7));
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: white;
  padding: 40px;
  text-align: center;
}

.overlay h2 {
  font-size: 32px;
  margin-bottom: 20px;
  font-weight: 500;
}

.overlay p {
  font-size: 16px;
  opacity: 0.9;
}

.login-right {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-form-container {
  width: 80%;
  margin: 0 auto;
}

.login-form-container h3 {
  font-size: 24px;
  color: #303133;
  margin-bottom: 10px;
  font-weight: 500;
}

.login-desc {
  color: #909399;
  margin-bottom: 30px;
}

.login-form {
  margin-top: 20px;
}

.login-form :deep(.el-input__inner) {
  height: 45px;
  line-height: 45px;
}

.remember-forgot {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forgot-btn {
  padding: 0;
  color: #909399;
}

.forgot-btn:hover {
  color: #409EFF;
}

.login-button {
  width: 100%;
  height: 45px;
  font-size: 16px;
  letter-spacing: 2px;
}

.login-footer {
  margin-top: 20px;
  text-align: center;
  color: #909399;
}

.register-btn {
  color: #409EFF;
  padding: 0;
}

.register-btn:hover {
  color: #66b1ff;
}

/* 响应式设计 */
@media screen and (max-width: 1024px) {
  .login-container {
    width: 90%;
    height: auto;
    min-height: 600px;
  }
}

@media screen and (max-width: 768px) {
  .login-box {
    flex-direction: column;
  }
  
  .login-left {
    display: none;
  }
  
  .login-right {
    padding: 30px 20px;
  }
  
  .login-form-container {
    width: 100%;
  }
}

.login-options {
  text-align: center;
  margin-top: 15px;
}
</style> 